package com.tycmc.signelectroniccontracts.tools.other;

import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.method.DigitsKeyListener;

/**
 * Created by gaoyong on 2018/3/8.
 */

public class MoneyNoMoreValueFilter extends DigitsKeyListener {
    private Double maxMoney;

    public MoneyNoMoreValueFilter() {
        super(false, true);
    }

    public MoneyNoMoreValueFilter setMaxMoney(Double maxMoney) {
        this.maxMoney = maxMoney;
        return this;
    }

    @Override
    public CharSequence filter(CharSequence source, int start, int end,
                               Spanned dest, int dstart, int dend) {
        CharSequence out = super.filter(source, start, end, dest, dstart, dend);
        StringBuffer stringBuffer =new StringBuffer(dest);
        stringBuffer.insert(dstart, source);
        try{
            Double aDouble = Double.valueOf(stringBuffer.toString());
            if(aDouble>maxMoney){
                return "";
            }
        }catch (Exception e){

        }

        return new SpannableStringBuilder(source, start, end);
    }


}
